<template>
  <el-dialog
    v-model="showClipDialog"
    title="创建拆条任务"
    width="600px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="clipFormRef" :model="clipForm" label-width="80px">
      <el-form-item label="拆条名称" prop="name" :rules="[{ required: true, message: '请输入拆条名称', trigger: 'blur' }]">
        <el-input v-model="clipForm.name" placeholder="请输入拆条名称" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime" :rules="[{ required: true, message: '请选择开始时间', trigger: 'change' }]">
        <el-input
          v-model="clipForm.startTime"
          placeholder="请输入开始时间（格式：HH:MM:SS）"
          @blur="updateStartTime"
        />
        <div class="form-tip">格式：HH:MM:SS，例如：01:30:00 表示1小时30分钟</div>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" :rules="[{ required: true, message: '请选择结束时间', trigger: 'change' }]">
        <el-input
          v-model="clipForm.endTime"
          placeholder="请输入结束时间（格式：HH:MM:SS）"
          @blur="updateEndTime"
        />
        <div class="form-tip">格式：HH:MM:SS，结束时间必须大于开始时间</div>
      </el-form-item>
      <el-form-item label="输出格式" prop="format">
        <el-select v-model="clipForm.format" placeholder="请选择输出格式">
          <el-option label="MP4" value="mp4" />
          <el-option label="HLS" value="hls" />
        </el-select>
      </el-form-item>
      <el-form-item label="转码模板" prop="transcodeTemplate">
        <el-select v-model="clipForm.transcodeTemplate" placeholder="请选择转码模板" clearable>
          <el-option
            v-for="template in transcodeTemplates"
            :key="template.id"
            :label="template.name"
            :value="template.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submitClip" :loading="clipping">
          {{ clipping ? '创建中' : '创建' }}
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { useRecordingClipping } from './useRecordingClipping'

// 使用录制拆条功能钩子
const {
  showClipDialog,
  selectedRecording,
  clipping,
  clipForm,
  transcodeTemplates,
  updateStartTime,
  updateEndTime,
  submitClip,
  resetClipForm
} = useRecordingClipping()

// 表单引用
const clipFormRef = ref(null)

// 处理关闭
const handleClose = () => {
  showClipDialog.value = false
  resetClipForm()
  // 重置表单验证
  if (clipFormRef.value) {
    clipFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>